package com.xjtu.platform.user.student.firstVisit;

import com.xjtu.platform.user.admin.dutyManage.DutyManageDto;
import com.xjtu.platform.common.*;
import com.xjtu.platform.common.model.FirstVisit;
import com.xjtu.platform.common.model.User;
import com.xjtu.platform.user.consultor.visitRecord.VisitRecordService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/student/firstVisit")
public class FirstVisitAPI {

    @Resource
    FirstVisitService firstVisitService;

    @Resource
    VisitRecordService visitRecordService;
    @Resource
    CommonService commonService;

    @RequestMapping(value = "/checkIsInProgress",method = RequestMethod.POST)
    public Result checkIsInProgress(@RequestBody DutyManageDto dto, @RequestHeader("Token") String Token){
        if(Token == null){
            System.out.println("查询当前用户时无令牌");
        }
        try {
            CurrentUser currentUser = TokenUtils.getUserInfo(Token, commonService);
            User user = new User();
            user.setUser_id(currentUser.getUserId());
            if(!firstVisitService.checkIsInProgress(user))
            {
                return Result.success("无未完成的咨询",Constants.NO_CONSULT_IN_PROGRESS);
            }else{
                return Result.fail(Constants.HAS_CONSULT_IN_PROGRESS,"有未完成的咨询");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail(400,"查询当前用户失败！");
    }

    @RequestMapping(value = "/submit",method = RequestMethod.POST)
    public Result checkIsInProgress(@RequestBody SubmitOrderDto dto, @RequestHeader("Token") String Token){
        if(Token == null){
            System.out.println("查询当前用户时无令牌");
        }
        try {
            CurrentUser currentUser = TokenUtils.getUserInfo(Token, commonService);
            dto.setUser_id(currentUser.getUserId());
            if(!firstVisitService.checkIsInProgress(dto))
            {
                firstVisitService.insertNewOrder(dto);
                return Result.success("提交成功");
            }else{
                return Result.fail(Constants.HAS_CONSULT_IN_PROGRESS,"有未完成的咨询");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail(400,"查询当前用户失败！");
    }

    @RequestMapping(value = "/getAllFirstVisit",method = RequestMethod.POST)
    public Result getAllFirstVisit(@RequestHeader("Token") String Token)
    {
        if(Token == null){
            System.out.println("查询当前用户时无令牌");
        }
        try {
            String user_id = TokenUtils.getUserInfo(Token,commonService).getUserId();
            FirstVisit dto = new FirstVisit();
            dto.setStudent_id(user_id);
            dto.setStatus("%");
            dto.setType("%");
            Map<String,Object> page = firstVisitService.getInfo(dto);
            return Result.success(page);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail(400,"查询当前用户失败！");
    }
}
